<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>FSM-golang | Abel&#39;Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="使用FSM（Finite State Machine）来制作怪物的AI，比较轻量级； 概念状态机分为两大类：    名称 说明    Moore machine 输出只和状态有关而与输入无关   Mealy machine 输出不仅和状态有关而且和输入有关系（使用这种状态机通常可以减少状态的数量。）   状态机可归纳为4个要素，即现态、条件、动作、次态。这样的归纳，主要是出于对状态机的内在因果关系">
<meta property="og:type" content="article">
<meta property="og:title" content="FSM-golang">
<meta property="og:url" content="http://example.com/2020/11/18/2020/2020-11-18-FSM-Golang/index.html">
<meta property="og:site_name" content="Abel&#39;Blog">
<meta property="og:description" content="使用FSM（Finite State Machine）来制作怪物的AI，比较轻量级； 概念状态机分为两大类：    名称 说明    Moore machine 输出只和状态有关而与输入无关   Mealy machine 输出不仅和状态有关而且和输入有关系（使用这种状态机通常可以减少状态的数量。）   状态机可归纳为4个要素，即现态、条件、动作、次态。这样的归纳，主要是出于对状态机的内在因果关系">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://s3.ax1x.com/2020/11/19/DulcAU.png">
<meta property="og:image" content="https://s3.ax1x.com/2020/11/19/DKZZLD.png">
<meta property="article:published_time" content="2020-11-17T16:00:00.000Z">
<meta property="article:modified_time" content="2021-07-28T09:12:55.331Z">
<meta property="article:author" content="Abel Sean">
<meta property="article:tag" content="golang">
<meta property="article:tag" content="Game">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s3.ax1x.com/2020/11/19/DulcAU.png">
  
    <link rel="alternate" href="/atom.xml" title="Abel&#39;Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Abel&#39;Blog</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-2020/2020-11-18-FSM-Golang" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/18/2020/2020-11-18-FSM-Golang/" class="article-date">
  <time datetime="2020-11-17T16:00:00.000Z" itemprop="datePublished">2020-11-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      FSM-golang
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>使用FSM（Finite State Machine）来制作怪物的AI，比较轻量级；</p>
<h1 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h1><p>状态机分为两大类：</p>
<table>
<thead>
<tr>
<th align="left">名称</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Moore machine</td>
<td align="left">输出只和状态有关而与输入无关</td>
</tr>
<tr>
<td align="left">Mealy machine</td>
<td align="left">输出不仅和状态有关而且和输入有关系（使用这种状态机通常可以减少状态的数量。）</td>
</tr>
</tbody></table>
<p>状态机可归纳为4个要素，即现态、条件、动作、次态。这样的归纳，主要是出于对状态机的内在因果关系的考虑。“现态”和“条件”是因，“动作”和“次态”是果。详解如下：</p>
<p>①现态：是指当前所处的状态。</p>
<p>②条件：又称为“事件”，当一个条件被满足，将会触发一个动作，或者执行一次状态的迁移。</p>
<p>③动作：条件满足后执行的动作。动作执行完毕后，可以迁移到新的状态，也可以仍旧保持原状态。动作不是必需的，当条件满足后，也可以不执行任何动作，直接迁移到新状态。</p>
<p>④次态：条件满足后要迁往的新状态。“次态”是相对于“现态”而言的，“次态”一旦被激活，就转变成新的“现态”了。</p>
<p>最首先状态机的概念是使用在电路，单片机相关的东西里面。</p>
<blockquote>
<p>在刚才归纳的要素里面很重要的就是“状态”，“条件“，”动作“。</p>
</blockquote>
<h1 id="游戏中常用的状态机"><a href="#游戏中常用的状态机" class="headerlink" title="游戏中常用的状态机"></a>游戏中常用的状态机</h1><p><a target="_blank" rel="noopener" href="https://imgchr.com/i/DulcAU"><img src="https://s3.ax1x.com/2020/11/19/DulcAU.png" alt="DulcAU.png"></a></p>
<table>
<thead>
<tr>
<th align="left">要素</th>
<th align="left">元素</th>
</tr>
</thead>
<tbody><tr>
<td align="left">状态</td>
<td align="left">待机状态</br>巡逻状态</br>追击状态</br>攻击状态</td>
</tr>
<tr>
<td align="left">条件</td>
<td align="left">出现敌人</br>主动怪判断</br>遭到攻击</br>攻击范围检测</br>追击范围检测</td>
</tr>
<tr>
<td align="left">动作</td>
<td align="left">朝某个位置移动</br>施法</td>
</tr>
</tbody></table>
<h1 id="阅读-looplab-fsm-实现"><a href="#阅读-looplab-fsm-实现" class="headerlink" title="阅读 looplab/fsm 实现"></a>阅读 looplab/fsm 实现</h1><p><a target="_blank" rel="noopener" href="https://imgchr.com/i/DKZZLD"><img src="https://s3.ax1x.com/2020/11/19/DKZZLD.png" alt="DKZZLD.png"></a></p>
<p>在创建状态机的时候需要指定状态之间切换的可能性；需要指定在状态切换的时候的调用。（状态是直接使用string来表示）</p>
<p>核心对象FMS，里面缓存了有限状态机的路由，并且能接受外部提供的Event的触发；</p>
<p>当Event区触发当前的有限状态机的时候，将会做状态的切换；</p>
<p>切换状态的时候，需要使用到 transitioner 对象；</p>
<p>参考实例代码：</p>
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    <span class="string">&quot;github.com/looplab/fsm&quot;</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// Door 门对象</span></span><br><span class="line"><span class="keyword">type</span> Door <span class="keyword">struct</span> &#123;</span><br><span class="line">    To  <span class="keyword">string</span></span><br><span class="line">    <span class="comment">// FSM 有限状态机对象</span></span><br><span class="line">    FSM *fsm.FSM</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// NewDoor 新建门对象</span></span><br><span class="line"><span class="comment">// to 名称</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">NewDoor</span><span class="params">(to <span class="keyword">string</span>)</span> *<span class="title">Door</span></span> &#123;</span><br><span class="line">    d := &amp;Door&#123;</span><br><span class="line">        To: to,</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    d.FSM = fsm.NewFSM(</span><br><span class="line">        <span class="comment">// 默认的状态名称</span></span><br><span class="line">        <span class="string">&quot;closed&quot;</span>,</span><br><span class="line">        fsm.Events&#123;</span><br><span class="line">            <span class="comment">// 事件 open ，能将状态机从 closed 转换成 open 状态</span></span><br><span class="line">            &#123;Name: <span class="string">&quot;open&quot;</span>, Src: []<span class="keyword">string</span>&#123;<span class="string">&quot;closed&quot;</span>&#125;, Dst: <span class="string">&quot;open&quot;</span>&#125;,</span><br><span class="line">            <span class="comment">// 事件 close ，能将状态机从 closed 转换成 open 状态</span></span><br><span class="line">            &#123;Name: <span class="string">&quot;close&quot;</span>, Src: []<span class="keyword">string</span>&#123;<span class="string">&quot;open&quot;</span>&#125;, Dst: <span class="string">&quot;closed&quot;</span>&#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">        fsm.Callbacks&#123;</span><br><span class="line">            <span class="comment">// 给状态机注册 enter_state 回调函数</span></span><br><span class="line">            <span class="string">&quot;enter_state&quot;</span>: <span class="function"><span class="keyword">func</span><span class="params">(e *fsm.Event)</span></span> &#123; d.enterState(e) &#125;,</span><br><span class="line">        &#125;,</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> d</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// enterState 当进入新状态的回调</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(d *Door)</span> <span class="title">enterState</span><span class="params">(e *fsm.Event)</span></span> &#123;</span><br><span class="line">    fmt.Printf(<span class="string">&quot;The door to %s is %s\n&quot;</span>, d.To, e.Dst)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">    door := NewDoor(<span class="string">&quot;heaven&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发起 open 事件</span></span><br><span class="line">    err := door.FSM.Event(<span class="string">&quot;open&quot;</span>)</span><br><span class="line">    <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">        fmt.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发起 close 事件</span></span><br><span class="line">    err = door.FSM.Event(<span class="string">&quot;close&quot;</span>)</span><br><span class="line">    <span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">        fmt.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>状态机本身只关心状态切换，他自己不关心这个事件生成；</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><ul>
<li>[1] <a target="_blank" rel="noopener" href="http://wiki.cnki.com.cn/HotWord/88305.htm">状态机概念</a></li>
<li>[2] <a target="_blank" rel="noopener" href="http://www.elecfans.com/emb/danpianji/2009020923861.html">状态机思路在单片机程序设计中的应用</a></li>
<li>[3] <a target="_blank" rel="noopener" href="https://github.com/looplab/fsm">looplab/fsm有限状态机实现</a></li>
<li>[4] <a target="_blank" rel="noopener" href="https://github.com/smallnest/gofsm">smallnest/gofsm有限状态机实现</a></li>
<li>[4] <a target="_blank" rel="noopener" href="https://www.cnblogs.com/sevenyuan/p/5276320.html">人工智能开发-决策方法</a></li>
<li>[5] <a target="_blank" rel="noopener" href="https://www.cnblogs.com/puputu/articles/1701012.html">erlang版本状态机</a></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2020/11/18/2020/2020-11-18-FSM-Golang/" data-id="ckrn3an75000t20xwfut06l4w" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Game/" rel="tag">Game</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/golang/" rel="tag">golang</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2020/11/21/2020/2020-11-21-cannon-guitarsheet/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          卡农吉他谱
        
      </div>
    </a>
  
  
    <a href="/2020/11/18/2020/2020-11-18-go%5B3%5D-%E6%8E%A5%E5%8F%A3/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">go[3]-接口</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/3D/" rel="tag">3D</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/AI/" rel="tag">AI</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/C/" rel="tag">C++</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Game/" rel="tag">Game</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Go/" rel="tag">Go</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Math/" rel="tag">Math</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Server/" rel="tag">Server</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Unreal-Engine4/" rel="tag">Unreal Engine4</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/" rel="tag">git</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/golang/" rel="tag">golang</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/guitar/" rel="tag">guitar</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/node-js/" rel="tag">node.js</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/" rel="tag">protobuf</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/recast/" rel="tag">recast</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/type/" rel="tag">type</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/vscode/" rel="tag">vscode</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E8%BF%90%E7%BB%B4/" rel="tag">运维</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/3D/" style="font-size: 10px;">3D</a> <a href="/tags/AI/" style="font-size: 11.67px;">AI</a> <a href="/tags/C/" style="font-size: 15px;">C++</a> <a href="/tags/Game/" style="font-size: 18.33px;">Game</a> <a href="/tags/Go/" style="font-size: 13.33px;">Go</a> <a href="/tags/Linux/" style="font-size: 13.33px;">Linux</a> <a href="/tags/Math/" style="font-size: 11.67px;">Math</a> <a href="/tags/Server/" style="font-size: 16.67px;">Server</a> <a href="/tags/Unreal-Engine4/" style="font-size: 10px;">Unreal Engine4</a> <a href="/tags/git/" style="font-size: 10px;">git</a> <a href="/tags/golang/" style="font-size: 20px;">golang</a> <a href="/tags/guitar/" style="font-size: 10px;">guitar</a> <a href="/tags/node-js/" style="font-size: 10px;">node.js</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/python/" style="font-size: 10px;">python</a> <a href="/tags/recast/" style="font-size: 11.67px;">recast</a> <a href="/tags/redis/" style="font-size: 10px;">redis</a> <a href="/tags/type/" style="font-size: 10px;">type</a> <a href="/tags/vscode/" style="font-size: 10px;">vscode</a> <a href="/tags/%E8%BF%90%E7%BB%B4/" style="font-size: 16.67px;">运维</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">April 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">March 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">January 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">December 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">November 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">March 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">November 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/07/19/2021/2021-07-19-etcd-learn/">go-etcd阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/07/19/2021/2021-07-19-socketop-learn/">REUSEPORT-REUSEADDR-学习</a>
          </li>
        
          <li>
            <a href="/2021/06/22/2021/2021-06-22-go-ethereum-learn/">go-ethereum阅读笔记</a>
          </li>
        
          <li>
            <a href="/2021/06/21/2021/2021-06-21-server%E6%B5%8B%E8%AF%95%E5%B7%A5%E5%85%B7/">server测试工具</a>
          </li>
        
          <li>
            <a href="/2021/06/09/2021/2021-06-09-pprof-doc/">go-pprof</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 Abel Sean<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>